package com.lhf.springcloud.controller;

import com.lhf.springcloud.Entity.teacher.TeacherData;
import com.lhf.springcloud.Entity.YearTeacher;
import com.lhf.springcloud.service.TeacherServiceImpl;
import com.lhf.springcloud.utils.ExcelUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@Controller
public class TeacherController {
    @Autowired
    private TeacherServiceImpl teacherService;
    @GetMapping("/teacherData")
    @ApiOperation("老师信息")
    public String listUploadedFiles() throws IOException {
        return "teacherData";
    }
    @RequestMapping(value = "/findAllTeacherData",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询所有老师信息")
    public Map findAllData(){
        List<TeacherData> allData = teacherService.findAllData();
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",allData.size());
        map.put("data",allData);
        return map;
    }
    @RequestMapping(value = "/findTeacherNum",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("查询所有老师信息的总条数")
    public Map findTeacherNum(){
        List<TeacherData> teacherNum = teacherService.findTeacherNum();
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",teacherNum.size());
        map.put("data",teacherNum);
        return map;
    }
    @RequestMapping(value = "/findTeacherNumByYear",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("通过入职年限查询该年老师信息总条数")
    public Map findTeacherNumByYear(){
        List<YearTeacher> teacherNum = teacherService.findTeacherNumByYear();
        Map map=new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("count",teacherNum.size());
        map.put("data",teacherNum);
        return map;
    }
    //@ApiOperation(value = "添加用户")
    @GetMapping("/teacherDownFiles")
    @ResponseBody
    @ApiOperation("下载文件到本地")
    public void downloadLocal(HttpServletResponse response) throws FileNotFoundException {
        // 下载本地文件
        String fileName = "教师信息表.xlsx".toString(); // 文件的默认保存名
        // 读到流中
        String property = System.getProperty("user.dir");
//        InputStream inStream = new FileInputStream("http://192.168.7.22:888/project/excel/老师信息表.xlsx");// 文件的存放路径
        InputStream inStream = new FileInputStream(property+"/excel/老师信息表.xlsx");// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //文件上传
    @ApiOperation("老师信息文件上传")
    @PostMapping("/teacherDownLoad")
    public String handleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes){
        if(file.isEmpty()) {
            return "redirect:/teacherData";
        }
        List<TeacherData> list1= new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            List<List<Object>> list = ExcelUtils.getCourseListByExcel(inputStream, file.getOriginalFilename());
            inputStream.close();

            for (int i = 1; i < list.size(); i++) {
                List<Object> courseList = list.get(i);

                TeacherData course = new TeacherData();
                if (courseList.size()==0){
                    break;
                }
                course.setTeacherName(courseList.get(0).toString());
                course.setIdCard(courseList.get(1).toString());
                course.setJobNumber(courseList.get(2).toString());
                course.setGender(courseList.get(3).toString());
//                if (Double.valueOf(String.valueOf(courseList.get(5)))>100||Double.valueOf(String.valueOf(courseList.get(5)))<0){
//                    redirectAttributes.addFlashAttribute("message", "第"+i+"行学生成绩异常，请核对后上传!" );
//                    list1.clear();
//                    break;
//                }
                list1.add(course);
                // 执行插入操作

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list1.size()>0){
            teacherService.addCourse(list1);
        }
        //redirectAttributes.addFlashAttribute("message", "You successfully uploaded " + file.getOriginalFilename() + "!" );

        return "redirect:/teacherData";  //重定向
    }
}
